<?php

namespace App\Http\Controllers;

use App\Datatables;
use App\IntegralCommodity;
use Illuminate\Http\Request;

class IntegralCommodityController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function commadityList(Request $request)
    {
        if ($request->isMethod('post')) {
            return Datatables::IntegralCommodityList();
        }

        return view('integralCommodity.commadityList');
    }

    /*
     * 修改商品
     */
    public function commadityDetail(Request $request,IntegralCommodity $integralCommodity)
    {
        if ($request->isMethod('post')) {
            $return = IntegralCommodity::CommodityModify($request,$integralCommodity);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }

        $infos = explode(',', $integralCommodity->infos);
        return view('integralCommodity.commadityDetail')->with(compact('integralCommodity', 'infos'));
    }


    /*
     * 添加商品
     */
    public function commadityPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = IntegralCommodity::CommodityPublish($request);
            if ($return['status'] === 1) {
                return redirect('/integralCommodity/list');
            } else {
                return back()->with('status', $return['status'])->with('message', $return['message']);
            }
        }

        return view('integralCommodity.commadityPublish');
    }

    /*
     * 删除
     */
    public function commadityDel(IntegralCommodity $integralCommodity)
    {
        if (!$integralCommodity->delete()) {
            return ['status'=> 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }
}
